import { getEnv } from "@/constants";

//清除右侧斜线
const rtrimSlash = (str: string) => str.replace(/\/$/g, '');

// 清除左侧斜线
const ltrimSlash = (str: string) => str.replace(/^\//g, '');

// 清除两侧斜线
const trimSlash = (str: string) => str.replace(/^\//g, '').replace(/\/$/g, '');

const formatApi = (url: string, prefix = "/") => {
  if (prefix != "/") prefix = "/" + trimSlash(prefix) + "/";
  return rtrimSlash(getEnv().apiDomain) + prefix + ltrimSlash(url);
}

/**
 * 格式化api接口
 */
export const toApi = (url: string) => formatApi(url, "/");

/**
 * 格式化comfyui api接口
 * @param url 
 * @returns 
 */
export const toComfyuiApi = (url: string) => formatApi(url, getEnv().app.comfyuiNotAuth ? "comfyui": "/ai/comfyui/");
